<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/15
 * Time: 11:41
 */

namespace app\service;


use Redis\Redis;
use think\Db;

class TradingService
{
    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易厅---买家确认购买 , 60分钟内未付款 , 扣除10金币 , 将交易状态改为出售中
     */
    function unpaid()
    {
        # 查询确认购买-60分钟内未付款的信息
        $info = Db::table('trading_room')->field('id,member_code,buyer_code,order_time')
            ->where(array('status' => 2))
            ->where('(' . time() . '-UNIX_TIMESTAMP(order_time))>= 3600')
            ->select();
        if (!empty($info)) {
            # 修改交易信息的状态 ,清空购买人的信息
            $_idInfo = implode(',', array_column($info, 'id'));
            if (!empty($_idInfo)) {
                Db::table('trading_room')->where('id in (' . $_idInfo . ')')
                    ->update(array(
                        'status' => 1,
                        'order_time' => null,
                        'buyer_code' => '',
                        'buyer_name' => '',
                        'buyer_head' => ''
                    ));
            }
            # 购买人扣除10金币,加记录
            $logArr = array();
            $redis = new Redis();
            foreach ($info as $k => $v) {
                Db::table('member_property')->where(array('member_code' => $v['buyer_code']))->setDec('gold', 10);
                $buyerInfo = $redis->hGetJson('memberInfo', $v['buyer_code']);
                $buyerInfo['gold'] = priceFormat($buyerInfo['gold'] - 10);
                $redis->hSetJson('memberInfo', $v['buyer_code'], $buyerInfo);
                # 记录
                array_push($logArr, array(
                    'member_code' => $v['buyer_code'],
                    'gold_num' => 10,
                    'type' => 26,
                    'create_time' => date('Y-m-d H:i:s'),
                    'memberp_code' => $v['member_code'],
                    'order_id' => $v['id']
                ));
            }
            # 添加记录
            if (!empty($logArr)) {
                Db::table('log_gold')->insertAll($logArr);
            }
        }
    }

    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @content 交易厅--卖家2小时  自动确认
     */
    function autoConfirm()
    {
        # 查询买家付款, 买家2小时未确认的交易信息
        $info = Db::table('trading_room')->field('id,member_code,buyer_code,num,type')
            ->where(array('status' => 3, 'report_status' => 1))
            ->where('(' . time() . '-UNIX_TIMESTAMP(pay_time))>= 7200')
            ->select();
        if (!empty($info)) {
            # 修改状态为已确认
            $_idInfo = implode(',', array_column($info, 'id'));
            Db::table('trading_room')->where('id in (' . $_idInfo . ')')->update(array('status' => 4, 'confirm_time' => date('Y-m-d H:i:s')));
            # 卖家加金币记录  买家加金币 , 金币记录 , 更新redis
            # 金币记录
            $logGoldArr = array();
            # 积分记录
            $logIntegralArr = array();
            $redis = new Redis();
            foreach ($info as $k => $v) {
                # 买家缓存信息
                $buyerInfo = $redis->hGetJson('memberInfo', $v['buyer_code']);
                if ($v['type'] == 1) {
                    # 金币
                    Db::table('log_gold')->where(array('member_code' => $v['member_code'], 'type' => 24, 'order_id' => $v['id']))->update(array('memberp_code' => $v['buyer_code']));
                    # 买家 加金币
                    Db::table('member_property')->where(array('member_code' => $v['buyer_code']))->setInc('gold', $v['num']);
                    $buyerInfo['gold'] = priceFormat($buyerInfo['gold'] + $v['num']);
                    # 买家金币记录
                    array_push($logGoldArr, array(
                        'member_code' => $v['buyer_code'],
                        'gold_num' => $v['num'],
                        'type' => 25,
                        'create_time' => date('Y-m-d H:i:s'),
                        'memberp_code' => $v['member_code'],
                        'order_id' => $v['id']
                    ));
                } else {
                    # 积分
                    # 卖家的积分记录
                    Db::table('log_integral')->where(array('member_code' => $v['member_code'], 'order_id' => $v['id'], 'type' => 17))->update(array('memberp_code' => $v['buyer_code']));
                    # 买家 加积分
                    Db::table('member_property')->where(array('member_code' => $v['buyer_code']))->setInc('integral', $v['num']);
                    $buyerInfo['integral'] = priceFormat($buyerInfo['integral'] + $v['num']);
                    # 买家积分记录
                    array_push($logIntegralArr, array(
                        'member_code' => $v['member_code'],
                        'num' => $v['num'],
                        'status' => 2,
                        'type' => 17,
                        'create_time' => date('Y-m-d H:i:s'),
                        'memberp_code' => $v['buyer_code'],
                        'order_id' => $v['id']
                    ));
                }
                # 更新买家redis
                $redis->hSetJson('memberInfo', $v['buyer_code'], $buyerInfo);
            }
            # 添加金币记录
            if (!empty($logGoldArr)) {
                Db::table('log_gold')->insertAll($logGoldArr);
            }
            # 添加积分记录
            if (!empty($logIntegralArr)) {
                Db::table('log_integral')->insertAll($logIntegralArr);
            }
        }

    }
}